import React from 'react';

import {
  getAPPListApi,
} from '@/api/Choreographer/index';

import {
  getScriptsApi,
} from '@/api/Choreographer/script';

const useEditPlaybook = () => {
  const [appList, setAppList] = React.useState();
  const [scriptList, setScriptList] = React.useState();

  const getAppList = (param) => {
    getAPPListApi(param).then((res) => {
      if (res.code === 200 && res.data.items) {
        setAppList(res.data.items);
      }
    });
  };

  const getScriptList = (param) => {
    getScriptsApi(param).then((res) => {
      if (res.code === 200 && res.data.items) {
        const list = Object.values(res.data.items).reduce((pre, cur) => {
          if (!cur.length) return pre;
          return pre.concat(cur);
        }, []);
        setScriptList(list);
      }
    });
  };


  React.useEffect(() => {
    getAppList({ limit: 9999 });
    getScriptList();
  }, []);

  return {
    appList,
    scriptList,
  };
};

export default useEditPlaybook;
